function [mattpixel,mat2d]=roiselect(pixelx,pixely,ref)
I=imread(ref);
I=rgb2gray(I);
Im=I;

%BW=roipoly(I,pixelx,pixely);
imshow(I)
h = imrect(gca);
position = wait(h);

positos=getPosition(h);
positos=round(positos);

%angulos inicales a optimizar 
r=0;
t=0;
%convert to radian
x=(pi*2*r)/360;
y=(pi*2*t)/360;
%traslacion 

T=[0 0 0];

countpixel=1;

error=int32(0);
%error=int16(0);

Isig=rgb2gray(imread('figuracuarto1.jpg'));
%imshow(BW);

%matriz de parametros intrinseco 
K=[1573.287920411348068 0 702.964654360824966;0 1587.526419194196706 627.725369611522524;0 0 1];
for i=positos(1,2):positos(1,2)+positos(1,4)
    for j=positos(1,1):positos(1,1)+positos(1,3)
        newpix=t2dto2d([j i 1],K);
        newpixworld=t2dto3d(newpix,10,[0,0,0],x,y);
        newpixworld2=t3dto3d(newpixworld,x,y,T);
        %[x
        % y
        % z]
        mattpixel(countpixel,:)=[newpixworld2(1,1) newpixworld2(2,1) newpixworld2(3,1)];
        %generar un dezplazamiento en busca de angulos de rotacion 
        %se genera un dezplazamiento x e y 
        
        newpixworldi=t3dto3d(newpixworld2',(pi*0/180),(pi*0/180),[0 0 0]);
       
        %--------------pinhole
        Xim2(1,1)=newpixworldi(1,1)/newpixworldi(3,1);
        Xim2(1,2)=newpixworldi(2,1)/newpixworldi(3,1);
        Xim2(1,3)=1;
        
        %----------------pixeles
        
        pixi=K*Xim2';
        pixi=abs(round(pixi));
        I(pixi(2,1),pixi(1,1))=Im(i,j);
        mat2d(countpixel,:)=[i j];
        intenI=int32(Im(i,j));
        intenIs=int32(Isig(pixi(2,1),pixi(1,1)));
        
        %fprintf('valor original %d  - valor siguiente %d  = %d \n',intenI,intenIs,((intenI-intenIs))/2);
        
        error = error + ((intenI-intenIs)^2)/2;
        Isig(pixi(2,1),pixi(1,1))=Im(i,j);
        
        %mattpixel(countpixel,:)=[pixi(2,1) pixi(1,1)];
        
        countpixel=countpixel+1;
        
        
        
    end
end
%mattpixel;
error



%figure, imshow(Im);
%figure, imshow(Isig);
end